package org.aplombh.java.leetcode.all;

public class _41缺失的第一个正数 {
    public static void main(String[] args) {
        System.out.println(new Solution41_1().firstMissingPositive(new int[]{1, 2, 0}));
    }
}

class Solution41_1 {
    public int firstMissingPositive(int[] nums) {

        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if (nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) {
                int tmp = nums[nums[i] - 1];
                nums[nums[i] - 1] = nums[i];
                nums[i] = tmp;
                i--;
            }
        }

        for (int i = 0; i < n; ++i) {
            if (nums[i] != i + 1) {
                return i + 1;
            }
        }
        return n + 1;
    }
}